package com.mofofa.maozy.loan

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import com.mofofa.maozy.base.BaseViewModel
import com.mofofa.maozy.data.ReqBase
import com.mofofa.maozy.network.ApiUserReposity
import com.mofofa.maozy.utils.AppConfigUtil
import com.mofofa.maozy.utils.UsageCountryUtil

/** 贷款viewModel */
class LoanViewModel : BaseViewModel() {

    /** 查询间隔多少天后能重新申请 */
    val onMaozyFetchMaozyNextMaozyApplyMaozyDaysMaozyLiveData =
        MutableLiveData<String>()

    /** 查询贷款额度信息 */
    private val mMaozyFetchMaozyLoanMaozyConfirmMaozyDetailMaozyLiveData =
        MutableLiveData<ReqBase>()

    /** 查询放款中信息 */
    private val mMaozyFetchMaozyLoaningMaozyDetailMaozyLiveData =
        MutableLiveData<ReqBase>()

    /** 创建贷款申请 */
    private val mMaozyCreateMaozyLoanMaozyLiveData = MutableLiveData<ReqBase>()

    /** 查询app评价流程 */
    private val mCheckMaozyAppMaozyReviewMaozyLiveData = MutableLiveData<ReqBase>()

    /** 查询贷款额度信息 */
    val onMaozyFetchMaozyLoanMaozyConfirmMaozyDetailMaozyLiveData =
        Transformations.switchMap(
            mMaozyFetchMaozyLoanMaozyConfirmMaozyDetailMaozyLiveData
        ) { query ->
            ApiUserReposity.fetchMaozyLoaningMaozyDetail(this, query)
        }

    /** 查询放款中信息 */
    val onMaozyFetchMaozyLoaningMaozyDetailMaozyLiveData =
        Transformations.switchMap(mMaozyFetchMaozyLoaningMaozyDetailMaozyLiveData) { query ->
            ApiUserReposity.fetchMaozyLoaningMaozyDetail(this, query)
        }

    /** 创建贷款申请 */
    val onMaozyCreateMaozyLoanMaozyLiveData =
        Transformations.switchMap(mMaozyCreateMaozyLoanMaozyLiveData) { query ->
            ApiUserReposity.createMaozyLoan(this, query)
        }

    /** 查询app评介流程 */
    val onCheckMaozyAppMaozyReviewMaozyLiveData =
        Transformations.switchMap(mCheckMaozyAppMaozyReviewMaozyLiveData) { query ->
            ApiUserReposity.checkMaozyAppMaozyReview(this, query)
        }

    /** 查询间隔多少天后能重新申请 */
    fun fetchMaozyNextMaozyApplyMaozyDays() {
        val nextApplyDays = AppConfigUtil.getCashArrwoReapplyMaozyDays()
        var nextApplyDaysString = "please apply again ${nextApplyDays} days later."
        if (UsageCountryUtil.isMaozyCountryMaozyCotediva()) {
            nextApplyDaysString = "veuillez réappliquer ${nextApplyDays} jours plus tard."

        } else if (UsageCountryUtil.isMaozyCountryMaozyVietnam()) {
            nextApplyDaysString = "vui lòng đăng ký lại sau ${nextApplyDays} ngày."
        }
        onMaozyFetchMaozyNextMaozyApplyMaozyDaysMaozyLiveData.value =
            nextApplyDaysString
    }

    /** 查询贷款额度信息 */
    fun fetchMaozyLoanMaozyConfirmMaozyDetail(reqBase: ReqBase) {
        mMaozyFetchMaozyLoanMaozyConfirmMaozyDetailMaozyLiveData.value = reqBase
    }

    /** 查询放款中信息 */
    fun fetchMaozyLoaningMaozyDetail(reqBase: ReqBase) {
        mMaozyFetchMaozyLoaningMaozyDetailMaozyLiveData.value = reqBase
    }

    /** 创建贷款申请 */
    fun createMaozyLoan(reqBase: ReqBase) {
        mMaozyCreateMaozyLoanMaozyLiveData.value = reqBase
    }
    /** 查询app评介流程 */
    fun checkMaozyAppMaozyReview(reqBase: ReqBase) {
        mCheckMaozyAppMaozyReviewMaozyLiveData.value = reqBase
    }
}